import  shelve
from config import database_path
from typing import Literal


{
    "action": "manage_table",
    "type": "add_table",
    "newtable": "new_table"
}
MANAGE_TABLE_TYPE=Literal["add_table", "remove_table", "rename_table", "copy_table",]

class manage_table:
    def __init__(self, config: dict) -> None:
        self.config=config
        type:MANAGE_TABLE_TYPE=config['type']
        
        
        if type == "add_table":
            self.add_table()
        elif type == "remove_table":
            self.remove_table()
        elif type == "rename_table":
            self.rename_table()
        elif type == "copy_table":
            self.copy_table()
    
    def add_table(self):
        with shelve.open(database_path,writeback=True) as db:
            db['tables_dict'][self.config['newtable']]=[]
    
    def remove_table(self):
        with shelve.open(database_path,writeback=True) as db:
            db['tables_dict'].pop(self.config['tablename'])
    
    def rename_table(self):
        with shelve.open(database_path,writeback=True) as db:
            
            
            db['tables_dict'][self.config['newtable']] = db['tables_dict'].pop(self.config['tablename'])

    
    def copy_table(self):
        with shelve.open(database_path,writeback=True) as db:
            
            
            db['tables_dict'][self.config['newtable']] =db['tables_dict'].get(self.config['tablename'],[])
        
